package com.ruicar.afs.cloud.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.product.entity.ProductPlanRate;
import com.ruicar.afs.cloud.product.mapper.ProductPlanRateMapper;
import com.ruicar.afs.cloud.product.service.ProductPlanRateService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author changyu.gui
 * @version 1.0
 * @date 2020/5/17 21:13
 */
@Service
public class ProductPlanRateServiceImpl extends ServiceImpl<ProductPlanRateMapper, ProductPlanRate> implements ProductPlanRateService {

    @Override
    public void updateProductPlanRate(List<ProductPlanRate> productPlanRateList, Long productPlanId) {
        this.baseMapper.deleteByProductPlanId(productPlanId);
        if (productPlanRateList.size() > 0) {
            productPlanRateList.forEach(rate -> {
                rate.setProductPlanId(productPlanId);
            });
            this.saveBatch(productPlanRateList);
        }
    }

    @Override
    public void copyProductPlanRate(Long id, Long copyId) {
        List<ProductPlanRate> list = new ArrayList<>();
        this.list(Wrappers.<ProductPlanRate>query().lambda().eq(ProductPlanRate::getProductPlanId, copyId)).stream().forEach(
                productPlanRate -> {
                    productPlanRate.setId(null);
                    productPlanRate.setProductPlanId(id);
                    list.add(productPlanRate);
                }
        );
        if (list.size() > 0) this.saveBatch(list);
    }
}
